#### Civilization 4 SDK Makefile 1.0 ####
##
## Based on DannyDaemonic's makefile
## http://forums.civfanatics.com/showthread.php?t=405444


#### Paths ####
TOOLKIT = C:\Program Files\Microsoft Visual C++ Toolkit 2003
PSDK = C:\Program Files\Microsoft Platform SDK for Windows Server 2003 R2
#PSDK= C:\Program Files\Microsoft SDKs\Windows\v7.0A

## Uncomment to make JP dll
JP = 1
!IFDEF JP
CIV = C:\Program Files\CYBERFRONT\Sid Meier's Civilization 4(J)\Beyond the Sword(J)
!ELSE
CIV = C:\Program Files\Firaxis Games\Sid Meier's Civilization 4\Beyond the Sword
!ENDIF

BOOST = $(CIV)\CvGameCoreDLL\Boost-1.32.0
PYTHON = $(CIV)\CvGameCoreDLL\Python24

#FFHSDK = %USERPROFILE%\My Documents\Civ\Mod Tool\SDK\CvGameCoreDLL2041o
#BOOST = $(FFHSDK)\Boost-1.32.0
#PYTHON = $(FFHSDK)\Python24

## Uncomment to have newly compiled dlls copied to your mod's Assets directory
#YOURMOD = C:\Program Files\Firaxis Games\Sid Meier's Civilization 4\Beyond the Sword\Mods\The History of Three Kingdoms

#### Tools ####
CC = "$(TOOLKIT)\bin\cl.exe"
CPP = "$(TOOLKIT)\bin\cl.exe"
LD = "$(TOOLKIT)\bin\link.exe"
RC = "$(PSDK)\bin\rc.exe"
## Uncomment to build dependencies using fastdep
FD = "$(MAKEDIR)\bin\fastdep.exe"

#### BLACKLIST ####
## Uncomment to block CvTextScreen (accidentally included by Firaxis)
BLACKLIST = CvTextScreens


#### Target Files ####
Debug_BIN = Debug\CvGameCoreDLL.dll
Release_BIN = Release\CvGameCoreDLL.dll

!IF [IF NOT EXIST CvGameCoreDLL.rc EXIT 1] == 0
Debug_RESOURCE = Debug\CvGameCoreDLL.res
Release_RESOURCE = Release\CvGameCoreDLL.res
!ENDIF

Debug_STATICLIB = Debug\CvGameCoreDLL.lib
Release_STATICLIB = Release\CvGameCoreDLL.lib

Debug_LIBDEF = Debug\CvGameCoreDLL.def
Release_LIBDEF = Release\CvGameCoreDLL.def

Debug_PCH = Debug\CvGameCoreDLL.pch
Release_PCH = Release\CvGameCoreDLL.pch

Debug_PDB = Debug\CvGameCoreDLL.pdb
Release_PDB = Release\CvGameCoreDLL.pdb

Debug_OTHER = Debug\CvGameCoreDLL.exp Debug\CvGameCoreDLL.ilk
Release_OTHER = Release\CvGameCoreDLL.exp

#### CFLAGS ####

## MODFLAGS ##
#   LOG_AI			enable BBAI AI logging
#   CYBERFRONT		for Japanese edition

#MODFLAGS = /DLOG_AI
!IFDEF MODFLAGS
!IFDEF JP
MODFLAGS = $(MODFLAGS) /DCYBERFRONT
!ENDIF
!ELSE
!IFDEF JP
MODFLAGS = /DCYBERFRONT
!ELSE
MODFLAGS =
!ENDIF
!ENDIF
###############

# /EHsc		catch C++ exceptions only; assume that extern C functions never throw a C++ exception
# /Gd		Use the __cdecl calling convention (x86 only). 
# /GR		enable run-time type information (RTTI)
# /Gy		enable function-level linking
# /G7		optimize for Pentium 4 and above
# /MD		multithreaded normal DLL with MSVCRT.dll
# /MDd		multithreaded debug DLL with MSVCRTD.dll
# /Od		disable optimizations
# /O2		optimize for speed (equal to /Og /Oi /Ot /Oy /Ob2 /Gs /GF /Gy)
# /W3		warning level 3

GLOBAL_CFLAGS = /GR /Gy /W3 /EHsc /Gd /DWIN32 /D_WINDOWS /D_USRDLL /DCVGAMECOREDLL_EXPORTS /Yu"CvGameCoreDLL.h" $(MODFLAGS)
#Debug_SAFE_CFLAGS = /MDd /Od /D_DEBUG /RTC1 /Fp"$(Debug_PCH)" $(GLOBAL_CFLAGS)
Debug_SAFE_CFLAGS = /MD /Od /D_DEBUG /RTC1 /Fp"$(Debug_PCH)" $(GLOBAL_CFLAGS)
Debug_CFLAGS = /Zi $(Debug_SAFE_CFLAGS)
Release_CFLAGS = /MD /O2 /Oy /Oi /G7 /DNDEBUG /DFINAL_RELEASE /Fp"$(Release_PCH)" $(GLOBAL_CFLAGS)

#### LDFLAGS ####
# /DEBUG	create debugging information
# /OPT:REF	remove unreferenced data/functions
# /OPT:ICF	identical COMDAT folding (trimming identical read-only data)
# /SUB:WIN	exe does not need a console, but this isn't an exe so unnecessary?
# /TLBID:1	override default type library ID (1), so this does nothing
# /LARGEADDRESSAWARE

GLOBAL_LDFLAGS = /DLL /NOLOGO /SUBSYSTEM:WINDOWS /LARGEADDRESSAWARE /TLBID:1
Debug_LDFLAGS = /INCREMENTAL /DEBUG /PDB:"$(Debug_PDB)" /IMPLIB:"$(Debug_STATICLIB)" $(GLOBAL_LDFLAGS) /NODEFAULTLIB:msvcprtd.lib
Release_LDFLAGS = /INCREMENTAL:NO /OPT:REF /OPT:ICF /PDB:"$(Release_PDB)" $(GLOBAL_LDFLAGS)

#### INCLUDES ####
GLOBAL_INCS = /I"$(TOOLKIT)/include" /I"$(PSDK)/Include" /I"$(PSDK)/Include/mfc"
PROJECT_INCS = /I"$(BOOST)/include" /I"$(PYTHON)/include"
Debug_INCS = $(PROJECT_INCS) $(GLOBAL_INCS)
Release_INCS = $(PROJECT_INCS) $(GLOBAL_INCS)

#### LIBS ####
GLOBAL_LIBS = /LIBPATH:"$(TOOLKIT)/lib" /LIBPATH:"$(PSDK)/Lib" winmm.lib user32.lib
PROJECT_LIBS = /LIBPATH:"$(BOOST)/libs" /LIBPATH:"$(PYTHON)/libs" boost_python-vc71-mt-1_32.lib
Debug_LIBS = $(PROJECT_LIBS) $(GLOBAL_LIBS) msvcprt.lib
Release_LIBS = $(PROJECT_LIBS) $(GLOBAL_LIBS)

#### Objects ####
Debug_LINKOBJS = $(Debug_OBJS)
Release_LINKOBJS = $(Release_OBJS)

#### Auto SOURCES/OBJS ####
!IF [ECHO SOURCES= \> sources.mk] == 0 && \
    [FOR %i IN (*.cpp) DO @ECHO. "%i" \>> sources.mk] == 0 && \
    [ECHO.>> sources.mk] == 0 && \
    [ECHO Debug_OBJS= \>> sources.mk] == 0 && \
    [FOR /F "delims=." %i IN ('dir /b *.cpp') DO @ECHO. Debug\%i.obj \>> sources.mk] == 0 && \
    [ECHO.>> sources.mk] == 0 && \
    [ECHO Release_OBJS= \>> sources.mk] == 0 && \
    [FOR /F "delims=." %i IN ('dir /b *.cpp') DO @ECHO. Release\%i.obj \>> sources.mk] == 0 && \
    [ECHO.>> sources.mk] == 0
!INCLUDE sources.mk
!IF [DEL sources.mk]
!ENDIF
!ENDIF

#### Targets ####
#################

.PHONY: all clean Debug_clean Release_clean Debug Release

all: Debug Release

clean: Debug_clean Release_clean 

Debug_clean:
	@FOR %i IN ($(Debug_BIN) $(Debug_STATICLIB) $(Debug_LIBDEF) \
		Debug\*.obj Debug\*.@ $(Debug_RESOURCE) \
		$(Debug_PCH) $(Debug_PDB) $(Debug_OTHER)) DO @IF EXIST "%i" DEL "%i"

Release_clean:
	@FOR %i IN ($(Release_BIN) $(Release_STATICLIB) $(Release_LIBDEF) \
		Release\*.obj Release\*.@ $(Release_RESOURCE) \
		$(Release_PCH) $(Release_PDB) $(Release_OTHER)) DO @IF EXIST "%i" DEL "%i"

Debug: Echo_MODFLAGS Debug_DIR Debug_unfinished $(Debug_PCH) $(Debug_BIN)
!IFDEF YOURMOD
	-COPY "$(Debug_BIN)" "$(YOURMOD)\Assets\."
!ENDIF

Release: Echo_MODFLAGS Release_DIR Release_unfinished $(Release_PCH) $(Release_BIN)
#!IFDEF YOURMOD
#	-COPY "$(Release_BIN)" "$(YOURMOD)\Assets\."
#!ENDIF

Echo_MODFLAGS:
	@ECHO MODFLAGS=$(MODFLAGS)

Debug_DIR:
	-@IF NOT EXIST "Debug\." MKDIR "Debug"

Release_DIR:
	-@IF NOT EXIST "Release\." MKDIR "Release"

Debug_unfinished:
	@ECHO.>Debug\unfinished.@
	@FOR /F "delims=@" %i IN ('dir /b Debug\*.@') DO \
		@IF EXIST "Debug\%i" DEL "Debug\%i"
	@FOR /F %i IN ('dir /b Debug\*.@') DO \
		@IF EXIST "Debug\%i" DEL "Debug\%i"

Release_unfinished:
	@ECHO.>Release\unfinished.@
	@FOR /F "delims=@" %i IN ('dir /b Release\*.@') DO \
		@IF EXIST "Release\%i" DEL "Release\%i"
	@FOR /F %i IN ('dir /b Release\*.@') DO \
		@IF EXIST "Release\%i" DEL "Release\%i"

$(Debug_BIN): $(Debug_LINKOBJS) $(Debug_RESOURCE)
	$(LD) /out:$(Debug_BIN) $(Debug_LDFLAGS) $(Debug_LIBS) $(Debug_LINKOBJS) $(Debug_RESOURCE)

$(Release_BIN): $(Release_LINKOBJS) $(Release_RESOURCE)
	$(LD) /out:$(Release_BIN) $(Release_LDFLAGS) $(Release_LIBS) $(Release_LINKOBJS) $(Release_RESOURCE)


.cpp{Debug}.obj:
	@ECHO.>"$*.obj.@"
    $(CPP) /nologo $(Debug_CFLAGS) $(Debug_INCS) /Fo$*.obj /c $<
	@DEL "$*.obj.@"

############ NO DEBUGGING INFORMATION ############
## If you got a "fatal error C1067", add obj and cpp file HERE. 
Debug\CyCityInterface1.obj: CyCityInterface1.cpp
	@ECHO.>"$*.obj.@"
    $(CPP) /nologo $(Debug_SAFE_CFLAGS) $(Debug_INCS) /Fo$@ /c CyCityInterface1.cpp
	@DEL "$*.obj.@"
#################################################

.cpp{Release}.obj:
	@ECHO.>"$*.obj.@"
    $(CPP) /nologo $(Release_CFLAGS) $(Release_INCS) /Fo$*.obj /c $<
	@DEL "$*.obj.@"

$(Debug_PCH) Debug\_precompile.obj:
	@ECHO.>"$(Debug_PCH).@"
	@ECHO.>"Debug\_precompile.obj.@"
	$(CPP) /nologo $(Debug_CFLAGS) $(Debug_INCS) /YcCvGameCoreDLL.h /Fo"Debug\_precompile.obj" /c _precompile.cpp
	@DEL "$(Debug_PCH).@"
	@DEL "Debug\_precompile.obj.@"

$(Release_PCH) Release\_precompile.obj:
	@ECHO.>"$(Release_PCH).@"
	@ECHO.>"Release\_precompile.obj.@"
    $(CPP) /nologo $(Release_CFLAGS) $(Release_INCS) /YcCvGameCoreDLL.h /Fo"Release\_precompile.obj" /c _precompile.cpp
	@DEL "$(Release_PCH).@"
	@DEL "Release\_precompile.obj.@"

.rc{Debug}.res:
	@ECHO.>"$*.res.@"
	$(RC) /Fo$@ $(Debug_INCS) $<
	@DEL "$*.res.@"

.rc{Release}.res:
	@ECHO.>"$*.res.@"
	$(RC) /Fo$@ $(Release_INCS) $<
	@DEL "$*.res.@"

!IFDEF BLACKLIST

Debug\$(BLACKLIST).obj: $(BLACKLIST).cpp
	@ECHO.>"$*.obj.@"
	@ECHO.>"$*-dummy.cpp"
	$(CPP) /nologo $(Debug_CFLAGS) $(Debug_INCS) /Y- /Fo$@ /c "$*-dummy.cpp"
	@DEL "$*-dummy.cpp"
	@DEL "$*.obj.@"

Release\$(BLACKLIST).obj: $(BLACKLIST).cpp
	@ECHO.>"$*.obj.@"
	@ECHO.>"$*-dummy.cpp"
	$(CPP) /nologo $(Release_CFLAGS) $(Release_INCS) /Y- /Fo$@ /c "$*-dummy.cpp"
	@DEL "$*-dummy.cpp"
	@DEL "$*.obj.@"

!ENDIF

!IFDEF FD

!IF [IF NOT EXIST $(FD) EXIT 1] == 0
!IF [$(FD) --objectextension=pch -q -O Debug CvGameCoreDLL.cpp > depends] != 0 || \
    [$(FD) --objectextension=obj -q -O Debug $(SOURCES) >> depends] != 0 || \
    [$(FD) --objectextension=pch -q -O Release CvGameCoreDLL.cpp >> depends] != 0 || \
    [$(FD) --objectextension=obj -q -O Release $(SOURCES) >> depends] != 0
!MESSAGE Error running fastdep.
!ENDIF
!ELSE
!IF [ECHO "fastdep.exe" NOT FOUND! && \
     ECHO Please edit Makefile to reflect the correct path of fastdep. && \
     ECHO. ]
!ENDIF
!ENDIF

!ENDIF

!IF EXIST(depends)
!INCLUDE depends
!ENDIF
